#pragma once

class CHttp
{
public:
	typedef struct _tagHttpInfo
	{
		CHttp* _http;
		void* _other;
	}HTTPINFO, *PHTTPINFO;

	typedef void (WINAPI *PFNHTTPCALLBACK)(PHTTPINFO, DWORD_PTR);

public:
	CHttp(void);
	virtual ~CHttp(void);

public:
	bool downfile(const char* lpszUrl, const char* lpszSavePath, const int numThreads = 10);
	void setcallback(PFNHTTPCALLBACK pfun, PHTTPINFO phttpinfo, DWORD_PTR dwParam) 
						{ m_pfnCallBack = pfun; m_pHttpInfo = phttpinfo; m_param = dwParam; }
	double getpercent();
	long getdownbytes();
	long getsize();

private:
	bool parsefilename(const char* lpszUrl, std::string& fileName, std::string& urlFilename);
	bool parsehostname(const char* lpszUrl, std::string& hostName, sockaddr_in& addr);
	bool getfilelength(size_t& fileLength, size_t& startPos);	
	bool singledown();


private:
	SOCKET m_socket;
	unsigned short m_port;
	sockaddr_in m_saUrl;
	std::string m_url;
	std::string m_urlfile;
	std::string m_hostName;
	std::string m_file;
	std::string m_savepath;	
	double m_rate;
	long m_fileLength;
	long m_remainder;
	DWORD_PTR m_param;
	PHTTPINFO m_pHttpInfo;
	PFNHTTPCALLBACK m_pfnCallBack;
};
